# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=automat
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgver=22.10.0
pkgrel=3
pkgdesc='Self-service finite-state machines for the programmer on the go (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
license=('MIT')
url='https://github.com/glyph/Automat'
depends=(
    ${MINGW_PACKAGE_PREFIX}-python
    ${MINGW_PACKAGE_PREFIX}-python-attrs
    ${MINGW_PACKAGE_PREFIX}-python-six
)
makedepends=(
    ${MINGW_PACKAGE_PREFIX}-python-setuptools
    ${MINGW_PACKAGE_PREFIX}-python-setuptools-scm
    ${MINGW_PACKAGE_PREFIX}-python-wheel
)
source=("https://pypi.org/packages/source/A/${_realname^}/${_realname^}-${pkgver}.tar.gz")
sha256sums=('e56beb84edad19dcc11d30e8d9b895f75deeb5ef5e96b84a467066b3b84bb04e')

prepare() {
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname^}-${pkgver}" "python-build-${MSYSTEM}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    # Remove shebang line
    sed -e '1 { s/^#!.*$// }' -i "${_f}"
  done
}
